package com.iscale.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.iscale.model.entity.BaseBrandInfo;
import com.iscale.model.entity.BaseFileInfo;
import com.iscale.model.entity.BaseNeedsInfo;
import com.iscale.mapper.BaseNeedsInfoMapper;
import com.iscale.service.BaseFileInfoService;
import com.iscale.service.BaseNeedsInfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.iscale.util.ResponseCode;
import com.iscale.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 需求类别 服务实现类
 * </p>
 *
 * @author Andychen
 * @since 2017-03-10
 */
@Service
public class BaseNeedsInfoServiceImpl extends ServiceImpl<BaseNeedsInfoMapper, BaseNeedsInfo> implements BaseNeedsInfoService {
    @Autowired
    BaseFileInfoService fileInfoService;


    @Override
    public Result saveNeeds(MultipartFile file,BaseNeedsInfo needsInfo) throws IOException {
        //保存
        boolean exits = checkNeedsByName(needsInfo.getName());
        if(exits)Result.error(ResponseCode.needs_exits_error);
        //上传文件
        if(null!=file){
            List<BaseFileInfo> fileInfoList = fileInfoService.saveFileInfos(new MultipartFile[]{file} ,needsInfo.getMerchantNo());
            if(null!=fileInfoList && fileInfoList.size()>0){
                needsInfo.setIconFile(fileInfoList.get(0).getId());
                needsInfo.setIconUrl(fileInfoList.get(0).getFileUrl());
            }
        }
        boolean result =  insert(needsInfo);
        if(result){
            return Result.success(needsInfo);
        }else{
            return Result.error(ResponseCode.add_needs_error);
        }
    }


    @Override
    public Result updateNeeds(MultipartFile file,BaseNeedsInfo needsInfo) throws IOException{
        //上传文件
        if(null!=file){
            List<BaseFileInfo> fileInfoList = fileInfoService.saveFileInfos(new MultipartFile[]{file} ,needsInfo.getMerchantNo());
            if(null!=fileInfoList && fileInfoList.size()>0){
                needsInfo.setIconFile(fileInfoList.get(0).getId());
                needsInfo.setIconUrl(fileInfoList.get(0).getFileUrl());
            }
        }
        boolean result =  updateById(needsInfo);
        if(result){
            return Result.success(needsInfo);
        }else{
            return Result.error(ResponseCode.add_needs_error);
        }
    }

    @Override
    public List<BaseNeedsInfo> selectBigNeeds() throws IOException {
        EntityWrapper<BaseNeedsInfo> entityWrapper = new EntityWrapper<>(new BaseNeedsInfo());
        entityWrapper.where("status={0}",0).andNew("pid={0}",0).or("pid is null");
        List<BaseNeedsInfo> ls =  selectList(entityWrapper);
        return ls;
    }

    @Override
    public boolean checkNeedsByName(String name){
        EntityWrapper<BaseNeedsInfo> entityWrapper = new EntityWrapper<>(new BaseNeedsInfo());
        entityWrapper.where("status={0}",0).and("name={0}",name.trim());

        int count = selectCount(entityWrapper);
        if(count>0)return  true;

        return  false;
    }
}
